//
//  ZBDrawingBoard.m
//  ZBDrawingTool
//
//  Created by apple on 2018/7/6.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "ZBDrawingBoard.h"
#import "ZBDrawPathModel.h"

@interface ZBDrawingBoard()

//路径的集合
@property (nonatomic, strong) NSMutableArray<ZBDrawPathModel *> *paths;

@end

@implementation ZBDrawingBoard

#pragma mark - 操作事件
#pragma mark - 操作事件

- (void)clean
{
    if(self.paths.count == 0){
        return;
    }
    [self.paths removeAllObjects];
    //重绘
    [self setNeedsDisplay];
    
}

- (void)back
{
    if(self.paths.count == 0){
        return;
    }
    [self.paths removeLastObject];
    //重绘
    [self setNeedsDisplay];
}

- (void)save
{
    if(self.paths.count == 0){
        return;
    }
    UIImage *image = [self makeImageWithView:self withSize:self.bounds.size];
    //保存到相册
    UIImageWriteToSavedPhotosAlbum(image, self, @selector(imageSavedToPhotosAlbum:didFinishWithError:contextInfo:), nil);
}
- (UIImage *)getDrawingImage
{
    if(self.paths.count == 0){
        return nil;
    }
    UIImage *image = [self makeImageWithView:self withSize:self.bounds.size];
    return image;
}

//保存图片的回调
- (void)imageSavedToPhotosAlbum:(UIImage *)image didFinishWithError:(NSError *)error contextInfo:(void *)contextInfo{
    NSString *message = @"";
    if (!error) {
        message = @"成功保存到相册";
    }else{
        message = [error description];
    }
    NSLog(@"message is %@",message);
}



#pragma mark - 重写系统的方法
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
   
    
    //1.获取触摸的任意对象
    UITouch *touch = [touches anyObject];
    
    //2.获取触摸的位置
    CGPoint point = [touch locationInView:touch.view];
    
    
    //3.创建起始点的路径
    ZBDrawPathModel *pathModel = [[ZBDrawPathModel alloc]init];
    
    
    //2.赋值开始点的属性
    if(self.lineWidth > 0){
        pathModel.lineWidth = self.lineWidth;
    }
    if(self.lineColor != nil){
        pathModel.pathColor = self.lineColor.CGColor;
    }
    //5.设置起点
    CGPathMoveToPoint(pathModel.path, &CGAffineTransformIdentity, point.x, point.y);
    
    //3.保存起点
    [self.paths addObject:pathModel];
    
    
}

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    //1.获取触摸的任意对象
    UITouch *touch = [touches anyObject];
    
    //2.获取触摸的位置
    CGPoint point = [touch locationInView:touch.view];
    
    //3.移动过程链接点增加到数组里面去
    ZBDrawPathModel *pathModel  = self.paths.lastObject;
    
    CGPathAddLineToPoint(pathModel.path, &CGAffineTransformIdentity,point.x, point.y);
    //4.绘制
    [self setNeedsDisplay];
    
}

- (void)drawRect:(CGRect)rect
{
    [super drawRect:rect];
    
    //1.获取图像上下文的画板和画笔
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    
    //2.设置每天路径的样式
    CGContextSetLineCap(ctx, kCGLineCapRound);
    CGContextSetLineJoin(ctx, kCGLineJoinRound);
    
    //3.遍历路径点开始渲染
    for (ZBDrawPathModel  *pathModel in self.paths) {
        
    
        CGContextSetLineWidth(ctx, pathModel.lineWidth);
        CGContextSetStrokeColorWithColor(ctx, pathModel.pathColor);
        
        //4.增加路径到画板
        CGContextAddPath(ctx, pathModel.path);
        
        //5.开始渲染
        CGContextStrokePath(ctx);  
    }
 
}

#pragma mark 生成image
- (UIImage *)makeImageWithView:(UIView *)view withSize:(CGSize)size
{
    
    // 下面方法，第一个参数表示区域大小。第二个参数表示是否是非透明的。如果需要显示半透明效果，需要传NO，否则传YES。第三个参数就是屏幕密度了，关键就是第三个参数 [UIScreen mainScreen].scale。
    //开启图片上下文
    UIGraphicsBeginImageContextWithOptions(size, NO, 0.0);
    
    //获取上下文
    CGContextRef context=UIGraphicsGetCurrentContext();
    //截屏
    [view.layer renderInContext:context];
    //获取图片
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    //关闭图片上下文
    UIGraphicsEndImageContext();
    return image;
}


#pragma mark - lazy
- (NSMutableArray<ZBDrawPathModel *> *)paths
{
    if(_paths == nil)
    {
        _paths = [NSMutableArray array];
    }
    return _paths;
}

@end
